﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;

namespace UCenter.ContactService.Email {
    public class Email163Service : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "163.com"; }
        }

        public string StartUrl {
            get { return "https://reg.163.com/logins.jsp?type=1&url=http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion

        #region Methods
        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            var nvc = new NameValueCollection();
            nvc.Add("password", pwd);
            nvc.Add("product", "mail163");
            nvc.Add("remUser", "on");
            nvc.Add("secure", "on");
            nvc.Add("selType", "-1");
            nvc.Add("style", "-1");
            nvc.Add("username", userName);
            nvc.Add("verifycookie", "1");
            string postContent = WebOpHelper.GetNameValueRowData(nvc, '&');

            var cookies = new NameValueCollection();
            cookies.Add("ntes_mail_firstpage", "normal");
            cookies.Add("Province", "010");

            string cks = WebOpHelper.GetNameValueRowData(cookies, ',');

            CookieContainer cc = new CookieContainer();
            cc.SetCookies(new Uri(StartUrl), cks);

            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.126.com/";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = StartUrl;
            string html = WebOpHelper.Post(StartUrl, accept, contentType, refer, language, postContent, cc, DefaultEncoding, out returnUrl);
            string url = WebOpHelper.Resove(html, "<link rel=\"stylesheet\" href=\"", "\" type=\"text/css\" />");
            string h = WebOpHelper.Post(url, accept, contentType, refer, language, "", cc, DefaultEncoding, out returnUrl);
            url = WebOpHelper.Resove(html, "window.location.replace(\"", "\")");
            html = WebOpHelper.Post(url, accept, contentType, refer, language, "", cc, DefaultEncoding, out returnUrl);
            Uri u = new Uri(returnUrl);
            string sid = WebOpHelper.GetQ(u, "sid");
            url = "http://" + u.Host + "/a/s?sid=" + sid + "&func=global:sequential";
            cookies = new NameValueCollection();       
            cookies.Add("mail_sid", sid);
            cookies.Add("mail_uid", userName + "@163.com");
            cookies.Add("MAIL163_SSN", userName);
            cookies.Add("NETEASE_AUTH_USERNAME", userName);
            cookies.Add("NETEASE_SSN", userName);
            cookies.Add("ntes_mail_firstpage", "normal");
            cookies.Add("nts_mail_user", userName + ":-1:1");
            cookies.Add("wmsvr_domain", u.Host);
            cookies.Add("mail_host", u.Host);
            cookies.Add("logType", "-1");   
            cks = WebOpHelper.GetNameValueRowData(cookies, ',');
            cc.SetCookies(new Uri(url), cks);
            postContent = "<?xml version=\"1.0\"?><object><array name=\"items\"><object><string name=\"func\">pab:searchContacts</string><object name=\"var\"><array name=\"order\"><object><string name=\"field\">FN</string><boolean name=\"ignoreCase\">true</boolean></object></array></object></object><object><string name=\"func\">user:getSignatures</string></object><object><string name=\"func\">pab:getAllGroups</string></object></array></object>";
            contentType = "application/xml";
            html = WebOpHelper.Post(url, accept, contentType, refer, language, postContent, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            System.Diagnostics.Debug.WriteLine(html);
            return WangyiResover.Resove(html);
        }
        #endregion
    }
}
